package com.controller;

import com.mapper.TopicMapper;
import com.pojo.Teacher;
import com.pojo.Topic;
import com.utils.Tool;
import org.apache.catalina.AccessLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 课程
@RestController
@CrossOrigin
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    TopicMapper tm;

    /**
     * 分页分类获取课题列表
     * @param type_id 分类 id
     * @param year 年份
     * @param page 页码
     * @param pagesize 单页数据量
     */
    @RequestMapping("/getList")
    public Map<String, Object> getList(Integer type_id,String year,Integer page,Integer pagesize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (type_id == null) {
            code = 500;
            msg = "type_id 不能为空";
        } else if (year == null) {
            code = 501;
            msg = "year 不能为空";
        } else {
            if (page == null) page = 0;
            if (pagesize == null) pagesize = 10;
            page *= pagesize;
            List<Topic> list = tm.getByTypePage(type_id,year,page,pagesize);
            for (Topic topic : list) {
                Teacher teacher = tm.getTeacherInfo(topic.getTeacher_id());
                teacher.setPassword(null);
                teacher.setLoginTime(null);
                topic.setTeacher(teacher);
            }
            map.put("total",tm.countTopic(type_id, year));
            map.put("data",list);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取课题详情
     * @param id 课题 id
     */
    @RequestMapping("/getDetails")
    public Map<String,Object> getDetails(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else {
            Topic topic = tm.getById(id);
            if (topic == null) {
                code = 501;
                msg = "课题不存在";
            } else {
                Teacher teacher = tm.getTeacherInfo(topic.getTeacher_id());
                teacher.setPassword(null);
                teacher.setLoginTime(null);
                topic.setTeacher(teacher);
                map.put("data",topic);
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 分页获取待审核课题
     * @param page 页码
     * @param pagesize 单页数据量
     */
    @RequestMapping("/getInspect")
    public Map<String, Object> getInspect(Integer page,Integer pagesize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (page == null) page = 0;
        if (pagesize == null) pagesize = 10;
        page *= pagesize;
        List<Topic> list = tm.getInspect(page,pagesize);
        for (Topic topic : list) {
            Teacher teacher = tm.getTeacherInfo(topic.getTeacher_id());
            teacher.setPassword(null);
            teacher.setLoginTime(null);
            topic.setTeacher(teacher);
        }

        map.put("code",code);
        map.put("msg",msg);
        map.put("data",list);
        map.put("total",tm.countInspect());
        return map;
    }

    /**
     * 获取教师发布的课题
     * @param teacher_id 教师 id
     * @param type 类型 0-获取所有课题 1-获取审核通过的课题
     */
    @RequestMapping("/getTeacherTopic")
    public Map<String, Object> getTeacherTopic(Integer teacher_id,Integer type,Integer page,Integer pagesize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (teacher_id == null) {
            code = 500;
            msg = "teacher_id 不能为空";
        } else if (type == null) {
            code = 501;
            msg = "type 不能为空";
        } else if (type > 1) {
            code = 502;
            msg = "type 参数无效";
        } else {
            if (page == null) page = 0;
            if (pagesize == null) pagesize = 10;
            page *= pagesize;
            List<Topic> list = null;
            Integer total = 0;
            if (type == 0) {
                list = tm.getTeacherAllTopic(teacher_id, page, pagesize);
                total = tm.countAll(teacher_id);
            } else {
                list = tm.getTeacherApprovedTopic(teacher_id,page,pagesize);
                total = tm.countByState(teacher_id);
            }
            for (Topic item : list) {
                item.setType_name(tm.getTypeNameById(item.getType_id()));
            }
            map.put("data",list);
            map.put("total",total);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    // 获取所有课题分类
    @RequestMapping("/getTypes")
    public Map<String, Object> getTypes() {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        map.put("code",code);
        map.put("msg",msg);
        map.put("data",tm.getTypes());
        return map;
    }

    /**
     * 添加课题分类
     * @param name 分类名
     */
    @RequestMapping("/addType")
    public Map<String, Object> addType(String name) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (name == null || name == "") {
            code = 500;
            msg = "name 不能为空";
        } else {
            tm.addType(name);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 修改课题分类
     * @param id 分类 id
     * @param name 分类名
     */
    @RequestMapping("/updateType")
    public Map<String, Object> updateType(Integer id,String name) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (name == null || name == "") {
            code = 501;
            msg = "name 不能为空";
        } else if (tm.getTypeById(id) == null) {
            code = 502;
            msg = "课题分类不存在";
        } else {
            tm.updateType(id, name);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 删除课题分类
     * @param id 分类 id
     */
    @RequestMapping("/delType")
    public Map<String, Object> delType(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (tm.getTypeById(id) == null) {
            code = 501;
            msg = "课题分类不存在";
        } else {
            tm.delType(id);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 审核课题
     * @param topic_id 课题 id
     * @param state 审核结果 1-拒绝 2-通过
     */
    @RequestMapping("/examine")
    public Map<String, Object> examine(Integer topic_id,Integer state) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (topic_id == null) {
            code = 500;
            msg = "topic_id 不能为空";
        } else if (state == null) {
            code = 501;
            msg = "state 不能为空";
        } else if (state < 1 || state > 2) {
            code = 502;
            msg = "state 不合法";
        } else {
            Topic topic = tm.getById(topic_id);
            if (topic.getState() != 0) {
                code = 503;
                msg = "该课题已审核";
            } else {
                tm.examine(topic_id, state);
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 添加课题
     * @param type_id 分类 id
     * @param teacher_id 教师 id
     * @param members 人数上限
     * @param title 标题
     * @param conents 内容
     * @param img 封面
     */
    @RequestMapping("/addTopic")
    public Map<String, Object> addTopic(Integer type_id,Integer teacher_id,Integer members,String title,String conents,String img) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (type_id == null) {
            code = 500;
            msg = "type_id 不能为空";
        } else if (teacher_id == null) {
            code = 501;
            msg = "teacher_id 不能为空";
        } else if (members == null) {
            code = 502;
            msg = "members 不能为空";
        } else if (title == null) {
            code = 503;
            msg = "title 不能为空";
        } else if (conents == null) {
            code = 504;
            msg = "conents 不能为空";
        } else {
            tm.addTopic(type_id, teacher_id, members, title, conents, img);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 修改课题
     * @param id 课题 id
     * @param type_id 分类 id
     * @param teacher_id 教师 id
     * @param members 人数上限
     * @param title 标题
     * @param conents 内容
     * @param img 封面
     */
    @RequestMapping("/updateTopic")
    public Map<String, Object> updateTopic(Integer id,Integer type_id,Integer teacher_id,Integer members,String title,String conents,String img) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (type_id == null) {
            code = 501;
            msg = "type_id 不能为空";
        } else if (teacher_id == null) {
            code = 502;
            msg = "teacher_id 不能为空";
        } else if (members == null) {
            code = 503;
            msg = "members 不能为空";
        } else if (title == null) {
            code = 504;
            msg = "title 不能为空";
        } else if (conents == null) {
            code = 505;
            msg = "conents 不能为空";
        } else {
            tm.updateTopic(id, type_id, teacher_id, members, title, conents, img);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 删除课题
     * @param id 课题 id
     */
    @RequestMapping("/delTopic")
    public Map<String, Object> delTopic(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (tm.getById(id) == null) {
            code = 501;
            msg = "课题不存在";
        } else {
            tm.delTopic(id);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 课题预约
     * @param team_id 小组 id
     * @param topic_id 课题 id
     */
    @RequestMapping("/subscribe")
    public Map<String,Object> subscribe(Integer team_id,Integer topic_id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (team_id == null) {
            code = 500;
            msg = "team_id 不能为空";
        } else if (topic_id == null) {
            code = 501;
            msg = "topic_id 不能为空";
        } else if (tm.getById(topic_id) == null) {
            code = 502;
            msg = "课题不存在";
        } else {
            tm.subscribe(team_id,topic_id);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 搜索课题
     * @param title 课题标题
     */
    @RequestMapping("/search")
    public Map<String, Object> search(String title) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (title == null) {
            code = 500;
            msg = "title 不能为空";
        } else {
            List<Topic> list = Tool.isNull(title) ? new ArrayList<>() : tm.search("%"+title+"%");
            for (Topic topic : list) {
                Teacher teacher = tm.getTeacherInfo(topic.getTeacher_id());
                teacher.setPassword(null);
                teacher.setLoginTime(null);
                topic.setTeacher(teacher);
            }
            map.put("data",list);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    // 获取课题年份
    @RequestMapping("/getYear")
    public Map<String,Object> getYear() {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        map.put("code",code);
        map.put("msg",msg);
        map.put("data",tm.getYears());
        return map;
    }

}
